// src/main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import '@/assets/less/index.less';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import { createPinia } from 'pinia';
import "@/api/mock.js";
import api from '@/api/api';
import { useAllDataStore } from "@/stores";

function isRoute(to){
  let res = router.getRoutes();
  let resFil= res.filter((item)=>item.path===to.path);
 return resFil.length>0;
}

router.beforeEach((to, from)=>{
  if(to.path!=="/login"&& !store.state.token){
    return { name: "login" }
  }
  if(!isRoute(to)){
    return { name: "404" };
  }
});
const pinia = createPinia();
const app = createApp(App);

app.use(pinia); // 确保 Pinia 在这里被初始化
app.config.globalProperties.$api = api;

const store = useAllDataStore(); // 现在可以安全地调用 useAllDataStore

store.addMenu(router, "refresh");
app.use(router);
app.use(ElementPlus);

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.mount('#app');